package mapper.Impl;


import mapper.CourseMapper;
import pojo.Course;
import utils.DBUtils;

import java.sql.SQLException;
import java.util.List;

public class CourseMapperImpl implements CourseMapper {
    @Override
    public void add(String name)  {
        try {
            DBUtils.commonUpdate("insert into course(name) values(?)",name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void delete(int id) {
        try {
            DBUtils.commonUpdate("delete from course where id = ?",id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void update(int id, String name) {
        try {
            DBUtils.commonUpdate("update course set name = ? where id = ?",name,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Course getCourse(int id) {

        Course course = null;
        try {
            course = DBUtils.commonQueryObj(Course.class,"select * from course where id = ?",id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return course;
    }

    @Override
    public Course getCourse(String name) {
        Course course = null;
        try {
            course = DBUtils.commonQueryObj(Course.class,"select * from course where name = ?",name);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return course;
    }

    @Override
    public List<Course> getCourses() {

        List<Course> courses = null;
        try {
            courses = DBUtils.commonQueryList(Course.class, "select * from course");
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return courses;
    }
}
